/**
 * 用于将path解析为面包屑
 */

import React from 'react';
import { Breadcrumb } from '@teamix/ui';
import PropTypes from 'prop-types';

const { pathResource } = window.FORCE;

export default class PathBreadcrumb extends React.Component {
  static propTypes = {
    startBreads: PropTypes.arrayOf(PropTypes.object),
    endBreads: PropTypes.arrayOf(PropTypes.object),
  };

  static defaultProps = {
    startBreads: [],
    endBreads: [],
  };

  render() {
    const { startBreads, endBreads } = this.props;

    let dataSource = [];

    if (startBreads && startBreads.length) {
      dataSource = [...startBreads];
    }

    const pathArr = pathResource.fullPath.split('/');

    let sumPath = '';
    pathArr.forEach((item) => {
      sumPath += `/${item}`;
      dataSource.push({
        link: sumPath,
        text: item,
      });
    });

    if (startBreads && startBreads.length) {
      dataSource = [...dataSource, ...endBreads];
    }

    return <Breadcrumb className="code-breadcrumb" dataSource={dataSource} separator={'/'} />;
  }
}
